package com.open.capacity.forum.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.forum.constant.MetasConstant;
import com.open.capacity.forum.model.Comment;
import com.open.capacity.forum.model.CommentPraise;
import com.open.capacity.forum.service.CommentService;
import com.open.capacity.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@Api(tags = "Comment API")
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping("blogComment")
    @ApiOperation(value = "博文评论分页条件查询")
    @LogAnnotation(module = "forum-center")
    public PageResult blogCommentPage(@RequestParam Map<String, Object> params) throws ControllerException {
        try {
            return commentService.findCommnet(MetasConstant.CommentType.ARTICLE, params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("blogComment")
    @ApiOperation(value = "博文评论提交")
    @LogAnnotation(module = "forum-center")
    public Result blogCommentSubmit(@RequestBody Comment comment) throws ControllerException {
        try {
            return commentService.commentSubmit(MetasConstant.CommentType.ARTICLE, comment);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("praise")
    @ApiOperation(value = "评论点赞")
    @LogAnnotation(module = "forum-center")
    public Result blogCommentSubmit(@RequestBody CommentPraise commentPraise) throws ControllerException {
        try {
            return commentService.commentPraise(commentPraise);
        } catch (Exception e) {
            throw new ControllerException(e);
        }
    }

}
